import requests

import time

headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Origin': 'http://59.215.206.188',
    'Proxy-Connection': 'keep-alive',
    'Referer': 'http://59.215.206.188/sxgl/mng/standardtongban/item/newreceiveindex?v=1757474580128',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'Cookie': 'JSESSIONID=B21B5D11A958BEE0D73274B3BBF574B0; wyhijack=/zhslzy/static/js/threejs/threejs.js_1756366033826; wyhtml=/sxgl/_1990d410cf681f5_1756863991030; tempTokenKey=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLnhormr4UiLCJhdWQiOiI5RTEwNTlDOTMzNUQ0QzdDQjhEQzc0MDBEMDNBRUNGQiIsInNlcnZlciI6Imh0dHA6Ly81OS4yMTUuMjA2LjE4OC9sb2dpbi8iLCJyb2xlIjoiUk9MRV9BUkVBLFJPTEVfV0lORE9XIiwiaXNJbml0aWFsUGFzc3dvcmQiOiIwIiwicmVnaW9uTmFtZSI6Iui0teW3nuecgSIsImluZHVzdHJ5IjpudWxsLCJhcHBDb2RlIjoiZGdvdi1wb3J0YWwiLCJvcmdhbk5hbWUiOiLmlrDov5vkurrlkZgiLCJyZWdpb25Db2RlIjoiNTIwMDAwIiwiaW5kdXN0cnlMaXN0IjpudWxsLCJvcmdhbkNvZGUiOiI1MjAwMDAyMDE3MDIxNjEwMDUzMDAwMDUwNyIsImV4cGlyYXRpb25UaW1lIjo1NSwiZ3JhZGUiOiIyIiwic3RhdHVzIjoiMSIsImlzcyI6Imluc3B1ciIsImlhdCI6MTc1NzQ2NjI0OCwiZXhwIjoxNzU3NDk1MDQ4fQ.nS7c-LqMBaNEIAsBO7sJaJkMs4fBByFOOKYEqNwea4fHDCkixyq_gUUdJ9kD-4w8K8X6zNB1NitoR97bFDk_gA; userInfoKey={%22role%22:%22ROLE_AREA%2CROLE_WINDOW%22%2C%22isInitialPassword%22:%220%22%2C%22regionName%22:%22%E8%B4%B5%E5%B7%9E%E7%9C%81%22%2C%22appCode%22:%22dgov-portal%22%2C%22organName%22:%22%E6%96%B0%E8%BF%9B%E4%BA%BA%E5%91%98%22%2C%22regionCode%22:%22520000%22%2C%22phone%22:%22384d53fa06a4844cfd54260b048c33e9%22%2C%22organCode%22:%2252000020170216100530000507%22%2C%22expirationTime%22:55%2C%22name%22:%221342b48e6f8f39ea7ba36accc05b61a8%22%2C%22id%22:%229E1059C9335D4C7CB8DC7400D03AECFB%22%2C%22email%22:%223f9980534589201ab2c5f2481db36392%22%2C%22username%22:%2247d57692a252657367001b6a2ff35333%22%2C%22status%22:%221%22%2C%22isWeakPwd%22:%220%22}; pwdState=true; inspurRoleKey=ROLE_AREA; windowId=0B7CA12E92004E0ABCA9EB88F0A792E4; James_cookie_sYQDUGqqzHtheme=custom',
}
current_milliseconds_timestamp = int(time.time() * 1000)
data_list=[]
#查询事项名
def query(事项名,目录名):

    params = {
        'isReceive': 'YES',
    }

    data = {
        '_search': 'true',
        'nd': current_milliseconds_timestamp,
        'rows': '10',
        'page': '1',
        'sidx': 'CUSTOMIZE_CODE',
        'sord': 'asc',
        'itemName': '',
        'basicCode': '',
        'authLevel': '',
        'itemType': '',
        'industry': '',
        'is_recieved': '',
        'templateItemName': 事项名,
        'customizeCode': '',
        'customizeCodeItem': '',
    }

    response = requests.post(
        'http://59.215.206.188/sxgl/mng/standardtongban/item/queryCatalogListForTemplate',
        params=params,

        headers=headers,
        data=data,
        verify=False,
    ).json()
    # print(response)
    #循环查询指定目录
    for i in response['rows']: #循环查询结果   也就是循环一级目录有几个

        #在此处区别是否有二级目录
        try:
            if response['records'] == 0:
                Exception: RuntimeError

            if i['cell']['NAME']==目录名:

                    展开一级目录(i['cell']['CODE'],事项名,目录名)
        except:
            data_list.append(事项名)
            print("异常了"+事项名)





def 展开一级目录(catalogCode,事项名,目录名):
    params = {
        'isReceive': 'YES',
        'catalogCode': catalogCode,
        'is_recieved': '',
    }

    data = {
        '_search': 'false',
        'nd': current_milliseconds_timestamp,
        'rows': '999',
        'page': '1',
        'sidx': 'CREATE_TIME',
        'sord': 'desc',
    }

    response = requests.post(
        'http://59.215.206.188/sxgl/mng/standardtongban/item/queryItemListForTemplateAndHandle',
        params=params,
        headers=headers,
        data=data,
        verify=False,
    ).json()
    # print(response)
    data=[]
    item_name=[]
    for i in response['rows']:
        if i['cell']['NAME']==事项名:
            # print(i['cell']['CODE'])
            data.append([事项名,目录名,i['cell']['CODE']])
            item_name.append(i['cell']['NAME'])

    if 事项名 not in item_name:
        data_list.append(事项名)
    # print(data)
    import 写入表格
    写入表格.write(data)
datas=[['单位参保信息变更登记', '单位参保信息变更登记']]
for i in datas:
    query(i[0],i[1])

print(data_list)